export const useCheckout = (): any => {
	/**
	 * 身份证校验
	 * @param val 传入的值
	 * @param type 1 是返回正则表达式 2是返回正则表达是的校验
	 * @returns boolean
	 */
	const checkCertno = (type: string = '1', val?: string): boolean | RegExp => {
		const reg =
			/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/;
		return type === '1' ? reg : reg.test(val ?? '');
	};

	/**
	 * 手机校验
	 * @param val
	 * @param type
	 * @returns boolean
	 */
	const checkPhone = (type: string = '1', val?: string): boolean | RegExp => {
		const reg = /^1[3456789]\d{9}$/;
		return type === '1' ? reg : reg.test(val ?? '');
	};

	/**
	 * 邮箱校验
	 * @param val
	 * @returns boolean
	 */
	const checkEmail = (type: string = '1', val?: string): boolean | RegExp => {
		const reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		return type === '1' ? reg : reg.test(val ?? '');
	};

	/**
	 * 中文校验
	 * @param val
	 * @param type
	 * @returns boolean
	 */
	const checkChinese = (type: string = '1', val?: string): boolean | RegExp => {
		const reg = /[\u4E00-\u9FA5]/;
		return type === '1' ? reg : reg.test(val ?? '');
	};

	/**
	 * 密码强度校验
	 * @param val
	 * @param type
	 * @returns boolean
	 */
	const checkPassword = (type: string = '1', val?: string): boolean | RegExp => {
		//密码强度正则，8-30位，包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符
		const reg = /^.*(?=.{8,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).{8,30}$/;
		return type === '1' ? reg : reg.test(val ?? '');
	};

	/**
	 * 根据身份证获取出生年月日
	 * @param idCard
	 * @returns
	 */
	const getBirthdayFromIdCard = (idCard: string) => {
		let birthday = '';
		if (idCard != null && idCard != '') {
			if (idCard.length == 15) {
				birthday = '19' + idCard.substr(6, 6);
			} else if (idCard.length == 18) {
				birthday = idCard.substr(6, 8);
			}
			birthday = birthday.replace(/(.{4})(.{2})/, '$1-$2-');
		}
		return birthday;
	};

	/**
	 * 身份证加密
	 * @param certno
	 * @returns
	 */
	const hideCertno = (certno: string): string => {
		if (!certno) {
			return '--';
		}
		return certno.replace(/(\d{3})\d{11}((\d{4})||(\d{3}\w{1))/, '$1***********$2');
	};

	interface ValidationRuleConfig {
		message: string;
		required?: boolean;
		trigger?: string;
		type?: string;
		// 其他可能的验证规则
	}

	// 获取规则提示
	const getValidRule = ({
		required = true,
		message,
		trigger = 'blur',
		type = '1'
	}: ValidationRuleConfig): { required: boolean; message: string; trigger: string } => {
		return { required, message: `${type === '1' ? '请输入' : '请选择'}${message}`, trigger: type === '1' ? trigger : 'change' };
	};

	return {
		checkCertno,
		checkPhone,
		checkEmail,
		checkChinese,
		checkPassword,
		getBirthdayFromIdCard,
		hideCertno,
		getValidRule
	};
};
